def Simple(n):
    d = 2
    while d < n:
        if n % d == 0:
            break
        else:
            d += 1
    if d == n:
        list.append(n)
        

n = int(input())
list = []
list.append(0)
if n < 1000 or n > 9999:
    print(0)
else:
    x1 = n 
    Simple(x1)
    x2 = n // 10
    Simple(x2)
    x3 = x2 // 10 
    Simple(x3)
    x4 = x3 // 10 
    Simple(x4)
    x5 = n - (x4 * 1000)
    Simple(x5)
    x6 = x3 - (x4*10)
    Simple(x6)
    x7 = x2 % 10 
    Simple(x7)
    x8 = x1 % 10 
    Simple(x8)
    x9 = x2 - (x4 * 100) 
    Simple(x9)
    x10 = x5 -(x6 * 100) 
    Simple(x10)
    print(max(list))


